package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.bean.Setmeal;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: 许志民
 * Date: 2021-11-27
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @CacheEvict(value = "SETMEAL_CACHE",key = "'SETMEAL'+#setmeal.status+'_'+'*'")
    @PostMapping
    public R addSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.addSetmeal(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几个", required = true),
            @ApiImplicitParam(name = "name", value = "条件", required = false)
    })
    public R getPage(Integer page,Integer pageSize,String name){
        Page<SetmealDto> getpage = setmealService.getPage(page,pageSize,name);
        return R.success(getpage);
    }
    @CacheEvict(value = "SETMEAL_CACHE",key = "'SETMEAL'+#setmeal.status+'_'+'*'")
    @DeleteMapping
    public R deleteIds(Long[] ids){
        log.info(ids.toString());
        boolean falg = setmealService.deleteIds(ids);
        return falg ? R.success("删除成功"):R.error("删除失败");
    }

    /**
     * 单个以及批量的停售和起售
     * @param status
     * @param ids
     * @return
     */
    @CacheEvict(value = "SETMEAL_CACHE",key = "'SETMEAL'+#setmeal.status+'_'+'*'")
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status,Long[] ids){
        if(status == null || ids == null){
            return R.error("修改失败");
        }
        boolean falg = setmealService.updateStatus(status,ids);
        return falg ? R.success("修改成功") : R.error("修改失败");
    }
    @GetMapping("/{id}")
    public R getSetmeal(@PathVariable Long id){
        if(id == null){
            return R.error("参数为空");
        }
        SetmealDto setmealDto = setmealService.getSetmealDto(id);
        return R.success(setmealDto);
    }
    @CacheEvict(value = "SETMEAL_CACHE",key = "'SETMEAL'+#setmeal.status+'_'+#setmealDto.categoryId")
    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDto setmealDto){
        if(setmealDto == null){
            return R.error("修改失败，不能为空");
        }
        boolean falg = setmealService.updateSetmeal(setmealDto);
        return falg ? R.success("修改成功") : R.error("修改失败");
    }

    @Cacheable(value = "SETMEAL_CACHE",key = "'SETMEAL'+#setmeal.status+'_'+#setmeal.categoryId")
    @GetMapping("/list")
    public R findList(Setmeal setmeal){
        List<Setmeal> setmeals = setmealService.findList(setmeal);
        return R.success(setmeals);
    }

    //setmeal/dish/1464490456720142337
    @GetMapping("/dish/{id}")
    public R setmealDish(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.setmealDish(id);
        return R.success(setmealDto.getSetmealDishes());
    }

}
